package com.ruoyi.web.controller.becompleted;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.BeCompleted;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.IBeCompletedService;
import com.ruoyi.system.service.IProCreateBuildingService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * 竣工资料Controller
 *
 * @author ruoyi
 * @date 2020-06-24
 */
@Controller
@RequestMapping("/ruoyi/completed")
public class BeCompletedController extends BaseController {
    private String prefix = "ruoyi/completed";

    @Autowired
    private IBeCompletedService beCompletedService;
    @Autowired
    private IProCreateBuildingService proCreateBuildingService;

    @RequiresPermissions("ruoyi:completed:view")
    @GetMapping()
    public String completed() {
        return prefix + "/completed";
    }

    /**
     * 详情页面传递参数
     * @param bId
     * @param map
     * @return
     */
    @RequiresPermissions("ruoyi:completed:view")
    @GetMapping("/completeds/{bId}")
    public String completeds(@PathVariable("bId") Long bId, ModelMap map) {
        BeCompleted beCompleted = beCompletedService.selectBeCompletedById(bId);
        map.put("beCompleted", beCompleted);
        return prefix + "/completeds";
    }

    /**
     * 查询总项目对应的竣工资料列表
     */
    @RequiresPermissions("ruoyi:completed:lists")
    @PostMapping("/lists")
    @ResponseBody
    public TableDataInfo list(Long sid) {
        startPage();
        List<BeCompleted> list = beCompletedService.selectBeCompletedLists(sid);
        return getDataTable(list);
    }

    /**
     * 首页查询对应的竣工资料
     * @param idss
     * @return
     */
    @RequiresPermissions("ruoyi:completed:listss")
    @PostMapping("/listss")
    @ResponseBody
    public TableDataInfo listss(Long idss) {
        startPage();
        List<BeCompleted> list = beCompletedService.selectBeCompletedLists(idss);
        return getDataTable(list);
    }


    /**
     * 查询竣工资料列表
     */
    @RequiresPermissions("ruoyi:completed:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo lists(BeCompleted beCompleted) {
        startPage();
        List<BeCompleted> list = beCompletedService.selectBeCompletedList(beCompleted);
        return getDataTable(list);
    }

    /**
     * 导出竣工资料列表
     */
    @RequiresPermissions("ruoyi:completed:export")
    @Log(title = "竣工资料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BeCompleted beCompleted) {
        List<BeCompleted> list = beCompletedService.selectUserexport(beCompleted);
        ExcelUtil<BeCompleted> util = new ExcelUtil<BeCompleted>(BeCompleted.class);
        return util.exportExcel(list, "竣工资料");
    }

    /**
     * 新增竣工资料
     */
    @GetMapping("/add/{pId}")
    public String add(@PathVariable("pId") Long pId, ModelMap map) {
        map.put("pId",pId);
        return prefix + "/add";
    }

    /**
     * 新增保存竣工资料
     */
    @RequiresPermissions("ruoyi:completed:add")
    @Log(title = "竣工资料", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("file") MultipartFile file, BeCompleted beCompleted) throws IOException {
        //上传文件路径
        String filePach= Global.getUploadPath();
        //上传并返回新文件名称
        String fileName= FileUploadUtils.upload(filePach,file);
        beCompleted.setBeData(fileName);

        String name = ShiroUtils.getLoginName();
        beCompleted.setBeTheHeir(name);
        //获取竣工资料 资料类型
        long beType=beCompleted.getBeType();
        //获取竣工资料外键
        long bePro=beCompleted.getBePro();
        //根据类型外键查询
        //List<BeCompleted> beCompleteds=beCompletedService.selectTypePro(beType,bePro);
        //如果竣工资料 资料类型等于6   修改主项目状态为2（已完成）
        if(beType==6){
            long pState=2;
            proCreateBuildingService.updateStatus(pState,bePro);
        }
        return toAjax(beCompletedService.insertBeCompleted(beCompleted));
    }

    /**
     * 修改竣工资料
     */
    @GetMapping("/edit/{beId}")
    public String edit(@PathVariable("beId") Long beId, ModelMap mmap) {
        BeCompleted beCompleted = beCompletedService.selectBeCompletedById(beId);
        mmap.put("beCompleted", beCompleted);
        return prefix + "/edit";
    }

    /**
     * 修改保存竣工资料
     */
    @RequiresPermissions("ruoyi:completed:edit")
    @Log(title = "竣工资料", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BeCompleted beCompleted) {
        return toAjax(beCompletedService.updateBeCompleted(beCompleted));
    }

    /**
     * 删除竣工资料
     */
    @RequiresPermissions("ruoyi:completed:remove")
    @Log(title = "竣工资料", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(beCompletedService.deleteBeCompletedByIds(ids));
    }

    /**
     *
     * 下载功能
     */
    @GetMapping("downloadFile")
    public void resourceDownload(@PathVariable("pUploading") String fAccessory, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // 本地资源路径
        String localPath = Global.getProfile();
        // 数据库资源地址
        String downloadPath = localPath + StringUtils.substringAfter(fAccessory, Constants.RESOURCE_PREFIX);
        // 下载名称
        String downloadName = StringUtils.substringAfterLast(downloadPath, "/");
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, downloadName));
        FileUtils.writeBytes(downloadPath, response.getOutputStream());
    }
}
